package org.apache.dolphinscheduler.api.controller;

import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.ProcessInstanceService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static org.apache.dolphinscheduler.api.enums.Status.QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR;

@Api(tags = "PROCESS_INSTANCE_TAG")
@RestController
@RequestMapping("projects/process-instances")
public class ProcessInstanceOtherController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProcessInstanceOtherController.class);

    @Autowired
    ProcessInstanceService processInstanceService;

    @GetMapping("/instance-num")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
//    @PreAuthorize("hasAnyAuthority('home:list')")
    public Result queryProcessInstanceNums(
            @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
            @RequestParam(value = "stateType", required = false) Integer stateType) {
        return processInstanceService.queryProcessInstanceNums(loginUser, stateType);
    }

    @GetMapping("/instance-num/month")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    public Result queryProcessInstanceNumsByMonth(
            @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "stateType", required = false) Integer stateType) {
        Map<String, Object> result = processInstanceService.queryProcessInstanceByMonth(year, loginUser, stateType);
        return returnDataList(result);
    }
}
